const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    verifyAccount: function (e) {
        this.setData({
            account: e.detail.value
        })
    },
    verifyPassword: function (e) {
        this.setData({
            password: e.detail.value
        })
    },
    bindGetUserInfo: function (e) {
        if (!e.detail.userInfo) {
            //用户按了拒绝按钮
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
                showCancel: false,
                confirmText: '确认',
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”')
                    }
                }
            })
        }
    },
    checkLogin: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=login',
            data: {
                username: self.data.account,
                password: self.data.password,
                client: 'wap'
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: (res) => {
                if (!res.data.datas.error) {
                    wx.setStorage({
                        key: 'key',
                        data: res.data.datas.key
                    })
                    wx.setStorage({
                        key: 'username',
                        data: res.data.datas.username
                    })
                    wx.setStorage({
                        key: 'member_mobile',
                        data: res.data.datas.member_mobile
                    })
                    self.getCartNum(res.data.datas.key);
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 2
                        })
                    }, 200)
                    wx.showLoading({
                        title: '登录中',
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
        });
    },

    //获取购物车商品数
    getCartNum: function (key) {
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_goods_count',
            data: {
                key: key
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    var result = res.data.datas;
                    if (result.cart_count > 0) {
                        wx.setTabBarBadge({
                            index: 3,
                            text: String(result.cart_count)
                        })
                    } else {
                        wx.removeTabBarBadge({
                            index: 3,
                        })
                    }
                    wx.setStorage({
                        key: 'cart_count',
                        data: result.cart_count
                    })
                }
            }
        });
    }
})